# program purpose
# This program is tring to fix the problem which Juan has for my training file 2013/01/16

# program inputs:

# program outputs:

# in production mode
inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/Posting_Oriented_Training_Dataset_2013_01_10.train_tfqAdded_labelsAdded_tail_500K_weiAllPreparedFeaturesAdded_wrong_lower_part_score"

# in debug mode
# inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/testing.in"

inputFileHandler = open(inputFileName,"r")
infoHeadLine = inputFileHandler.readline()

queryIDMetaInfoDict = {}

inputFileName2 = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/resultsNeededToAnalyzeAccessAuxFile_20130116.txt"
inputFileHandler2 = open(inputFileName2,"r")
for index,line in enumerate( inputFileHandler2.readlines() ):
    lineElements = line.strip().split(" ")
    queryID = lineElements[0]
    if index+1 <= 147:
        queryID = str( int(queryID) + 100000 )
    queryMetaInfoLine = lineElements[1] + " " + lineElements[2] + " " + lineElements[3]
    # print "queryID:",queryID
    
    if queryID not in queryIDMetaInfoDict:
        queryIDMetaInfoDict[queryID] = queryMetaInfoLine
    else:
        print "system error"
        print "queryID:",queryID
        # print "queryIDMetaInfoDict:",queryIDMetaInfoDict
        exit()

print "len(queryIDMetaInfoDict):",len(queryIDMetaInfoDict)
inputFileHandler2.close()

# in production
inputFileName3 = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/resultsNeededToAnalyze.txt"
inputFileHandler3 = open(inputFileName3,"r")


# in production
outputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/Posting_Oriented_Training_Dataset_2013_01_10.train_tfqAdded_labelsAdded_tail_500K_weiAllPreparedFeaturesAdded_fixed_20130116"
# in debug
# outputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/testing.out"
outputFileHandler = open(outputFileName,"w")
outputFileHandler.write(infoHeadLine)

previousQid = ""
previousTrecID = ""



for index,currentLine in enumerate( inputFileHandler.readlines() ):
    
    currentQueryQueryTermPositionIndexDict = {}
    currentReadLineElements = []

    currentLineElements = currentLine.strip().split(" ") 
    
    qid = currentLineElements[0]
    trecID = currentLineElements[1]
    term = currentLineElements[2]
    partialBM25 = currentLineElements[3]
    term_freq_in_collection = currentLineElements[4]
    term_freq_in_doc = currentLineElements[5]
    doc_words = currentLineElements[6]
    overallBM25Score = currentLineElements[7]
    rank_in_this_results_list_for_this_query = currentLineElements[8]
    
    print "index:",index,"qid:",qid,"trecID:",trecID
    #print "checking qid:",qid
    #print "checking trecID:",trecID
    
    
    print "retrieve info from file"
    previousQid = qid
    previousTrecID = trecID
    
    # 76580 28147 2369133198 2369169341
    queryIDMetaInfo = queryIDMetaInfoDict[qid]
    print "queryIDMetaInfo:",queryIDMetaInfo
    
    queryIDMetaInfoElements = queryIDMetaInfo.strip().split(" ")
    totalNumberOfResults = queryIDMetaInfoElements[0]
    resultsBeginningPosition = int( queryIDMetaInfoElements[1] )
    resultsEndingPosition = int( queryIDMetaInfoElements[2] )
    inputFileHandler3.seek(resultsBeginningPosition)
    
    currentReadLine = inputFileHandler3.readline()
    print "currentReadLine:",currentReadLine.strip()
    
    # this line should be the position index of each term if the IR system return some results
    lineElements = currentReadLine.strip().split(" ")
    
    for element in lineElements:
        currentQueryTerm = element.strip().split(":")[0]
        currentQueryTermPositionIndex = int( element.strip().split(":")[1] )
        if currentQueryTerm not in currentQueryQueryTermPositionIndexDict:
            currentQueryQueryTermPositionIndexDict[currentQueryTerm] = currentQueryTermPositionIndex
    
    # print "currentQueryQueryTermPositionIndexDict:",currentQueryQueryTermPositionIndexDict
    
    while currentReadLine.strip().split(" ")[-1] != trecID:
        currentReadLine = inputFileHandler3.readline()
    # print currentReadLine.strip()
    
    currentReadLineElements = currentReadLine.strip().split(" ")
    print

    

    # for index,element in enumerate( currentReadLineElements ):
    #    print index,element
    partialBM25_correct = currentReadLineElements[1 + currentQueryQueryTermPositionIndexDict[term] ]
    term_freq_in_collection_correct = currentReadLineElements[1 + currentQueryQueryTermPositionIndexDict[term] + 10]
    term_freq_in_doc_correct = currentReadLineElements[1 + currentQueryQueryTermPositionIndexDict[term] + 10 + 10]
    doc_words_correct = currentReadLineElements[-3]
    overallBM25Score_correct = currentReadLineElements[-2]
    rank_in_this_results_list_for_this_query_correct = currentReadLineElements[0]
    
    # print "partialBM25_correct:",partialBM25_correct
    # print "term_freq_in_collection_correct:",term_freq_in_collection_correct
    # print "term_freq_in_doc_correct:",term_freq_in_doc_correct
    # print "doc_words_correct:",doc_words_correct
    # print "overallBM25Score_correct:",overallBM25Score_correct
    # print "rank_in_this_results_list_for_this_query_correct:",rank_in_this_results_list_for_this_query_correct
    
    # re-assign the correct values
    currentLineElements[3] = partialBM25_correct
    currentLineElements[4] = term_freq_in_collection_correct
    currentLineElements[5] = term_freq_in_doc_correct
    currentLineElements[6] = doc_words_correct
    currentLineElements[7] = overallBM25Score_correct
    currentLineElements[8] = rank_in_this_results_list_for_this_query_correct        

    newoutputLine = ""
    
    for element in currentLineElements:
        newoutputLine += element + " "
    
    newoutputLine = newoutputLine.strip() + "\n"
    outputFileHandler.write( newoutputLine )
    
    # print "oldLine:",currentLine.strip()
    # print "newLine:",newoutputLine.strip()


inputFileHandler.close()
inputFileHandler3.close()
outputFileHandler.close()

















